#!/usr/bin/env python
# encoding: utf-8
# Date: 2017-06-18 17:09:08
# file:views.py
# Email: wang521kang@outlook.com
# Author: 王康
import json
from django import http


def request_parms_check(**parms):
    def request_parms(func):
        def get_request_parms(request, *args, **kwargs):
            for key in parms.keys():
                if request.POST.has_key(key):
                    kwargs[key] = request.POST[key]
                elif request.GET.has_key(key):
                    kwargs[key] = request.GET[key]
                value = parms[key]
                l = value[1].split(',')
                for s in l:
                    if s == 'r':
                        if not kwargs[key]:
                            return get_json(False, '%s 不能为空' % value[0])
                    if s == 'int':
                        if kwargs[key]:
                            try:
                                kwargs[key] = int(kwargs[key])
                            except:
                                return get_json(False, '%s 是数字类型' % value[0])

            return func(request, *args, **kwargs)

        return get_request_parms

    return request_parms


def get_json(success=False, message='', result=None):
    date = {}
    date['success'] = success
    date['message'] = message
    date['result'] = result
    html = json.dumps(date)
    return http.HttpResponse(html)
